package com.everbest.admin.utils;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.TimeZone;

/**
 * LocalDateTime 相关工具
 */
public class LocalDateTimeUtil {

    // 获取当前时间戳 : 秒
    public static Long currentTimestamp() {
        return LocalDateTime.now().atZone(ZoneId.systemDefault()).toEpochSecond();
    }

    // 获取当前时间戳 : 毫秒
    public static Long currentTimestampMilli() {
        return LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }

    // 获取前一天此刻的时间戳
    public static long yesterdayTimeStamp(long timestamp) {
        // 时间戳转 LocalDateTime
        Instant instant = Instant.ofEpochMilli(timestamp);
        ZoneId zone = ZoneId.systemDefault();
        // 拿到前一天 LocalDateTime
        LocalDateTime yesterday = LocalDateTime.ofInstant(instant, zone).plusDays(-1);
        // LocalDateTime 转时间戳
        Instant before = yesterday.atZone(zone).toInstant();
        return before.toEpochMilli();
    }

    // 计算今天零点的时间戳 timestamp 为毫秒
    public static long todayStartTimeStamp(long timestamp) {
        return timestamp - ((timestamp + TimeZone.getDefault().getRawOffset()) % (24 * 60 * 60 * 1000L));
    }

    // 将LocalDateTime转为自定义的时间格式的字符串
    public static String getDateTimeAsString(LocalDateTime localDateTime, String format) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
        return localDateTime.format(formatter);
    }

    // 将long类型的timestamp转为LocalDateTime
    public static LocalDateTime getDateTimeOfTimestamp(long timestamp) {
        Instant instant = Instant.ofEpochMilli(timestamp);
        ZoneId zone = ZoneId.systemDefault();
        return LocalDateTime.ofInstant(instant, zone);
    }

    // 将LocalDateTime转为long类型的timestamp
    public static long getTimestampOfDateTime(LocalDateTime localDateTime) {
        ZoneId zone = ZoneId.systemDefault();
        Instant instant = localDateTime.atZone(zone).toInstant();
        return instant.toEpochMilli();
    }

    // 将某时间字符串转为自定义时间格式的LocalDateTime
    public static LocalDateTime parseStringToDateTime(String time, String format) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
        return LocalDateTime.parse(time, df);
    }

    // LocalDate -> Date
    public static Date toDate(LocalDate localDate) {
        return Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
    }

    // LocalDateTime -> Date
    public static Date toDate(LocalDateTime localDateTime) {
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    // Date -> LocalDate
    public static LocalDate toLocalDate(Date date) {
        return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDate();
    }

    // Date -> LocalDateTime
    public static LocalDateTime toLocalDateTime(Date date) {
        return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime();
    }
}
